package com.greenlife.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.greenlife.common.enums.BaseResultCodeEnum;
import com.greenlife.common.result.CommonResult;
import com.greenlife.common.result.SingleResult;
import com.greenlife.controller.util.ControllerErrorProcessor;
import com.greenlife.service.FileServerClient;

@Controller
@RequestMapping("file/")
public class FileController {

	@Autowired
	private FileServerClient fileServerClient;
	Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
	 * 5.1.上传文件
	 * 
	 * @param file
	 * @return
	 */
	@RequestMapping(value = "upload", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult image(@RequestParam("file") MultipartFile file) {
		try {
			String url = fileServerClient.uploadFile(file);
			SingleResult<String> result = new SingleResult<String>(BaseResultCodeEnum.ERR_CODE_200, url);
			return result;
		} catch (Exception ex) {
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}

}
